import { useEffect, useState } from "react";
import {throttle} from 'underscore';

export default function usePosition(){
  // 状态来记录位置
  const [scrollX, setScrollX] = useState(0)
  const [scrollY, setScrollY] = useState(0)
 
  useEffect(()=>{
    const scrollHandler = throttle(() => {
      setScrollX(window.scrollX)
      setScrollY(window.scrollY)
    }, 200)

    window.addEventListener('scroll', scrollHandler )
    return ()=>{
      window.removeEventListener('scroll',scrollHandler)
    }
  }, [] )
  return { scrollX, scrollY }
}